package tas.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import tas.dao.DaoUsuarios;
import tas.dao.impl.DaoUsuariosImpl;
import tas.domain.Usuarios;

@WebServlet(name = "ServletUsuarios", urlPatterns = {"/Usuarios"})
public class ServletUsuarios extends HttpServlet {

    protected void processRequest(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {

        String accion = request.getParameter("accion");
        String result = null;
        String target = null;

        if (accion == null) {
            result = "Solicitud no recibida";

        } else if (accion.equals("LOGIN")) {
            String correo = request.getParameter("correo");
            String password = request.getParameter("password");

//            if ((correo == null) || (correo.trim().length() == 0) 
//                    || !Valida.esCorreo(correo)) {
//                result = "Correo Incorrecto";
//            }

            if (result == null) {
                if ((password == null) || (password.trim().length() == 0)) {
                    result = "Password Incorrecto";
                }
            }

            if (result == null) {
                DaoUsuarios daoUsuarios = new DaoUsuariosImpl();
                Usuarios u = daoUsuarios.autentica(correo, password);
              
                String IPConectado = InetAddress.getLocalHost().getHostAddress();
                String PCConectado = InetAddress.getLocalHost().getHostName(); 
                 
               request.getSession().setAttribute("IP", IPConectado);
               request.getSession().setAttribute("PC", PCConectado);
               
               
                if (u == null) {
                    result = "Usuario no Registrado";
                } else {
                    final String ID = request.getSession().getId();
                    request.getSession().setAttribute("ID", ID);
                    request.getSession().setAttribute("usuario", u);
                    request.getSession().setAttribute("autorizacion", u.getAutorizacion());
                    

                    if (u.getAutorizacion().equals("ADMIN")) {
                        result = "view/admin/";
                    } else if (u.getAutorizacion().equals("TRABA")) {
                        result = "view/traba/";
                    } else if (u.getAutorizacion().equals("OPERA")) {
                        result = "view/opera/";
                    }
                }
            }

        } else if (accion.equals("LOGOUT")) {
            request.getSession().removeAttribute("ID");
            request.getSession().removeAttribute("usuario");
            request.getSession().invalidate();

            target = "view/";

        } else {
            result = "Solicitud no reconocida";
        }

        if (result != null) {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.print(result);
            out.close();
        }

        if (target != null) {
            response.sendRedirect(target);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="doGet y doPost">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
